$(function () {
    var devId = getQueryString('devId');
    var devType = getQueryString('devType');
    var queryParam = '';
    if (devId) {
        queryParam += 'devId=' + devId;
        vm.devId = devId;
    }
    if (devType) {
        vm.devType = devType;
        queryParam += (queryParam ? '&' : '') + 'devType=' + devType;
    }
    var queryUrl = '../specialdevcheckrecord/list';
    if (queryParam) {
        queryUrl += '?' + queryParam;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {
                label: '校验日期', name: 'checkDate', index: 'CHECK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '校验机构名称', name: 'inspectUnitName', index: 'INSPECT_UNIT_ID', width: 120},
            {label: '检测报告编号', name: 'certificateNumber', index: 'CERTIFICATE_NUMBER', width: 120},
            {label: '录入人', name: 'recorderName', index: 'NICK_NAME', width: 80},
            {
                label: '录入时间', name: 'gmtCreate', index: 'GMT_CREATE', width: 80,
                formatter: function (v) {
                    return transDate(v, 'yyyy-MM-dd hh:mm:ss')
                }
            },
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {
                label: '校验证书', width: 80, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button title='查看' class='btn btn-outline btn-info' onclick='vm.goEquFile(\"" + row.id + "\")'><i class='fa fa-file-text-o'></i> 查看</button>";
                }
            },
            {name: 'devId', hidden: true},
            {name: 'id', index: 'ID', key: true, hidden: true}
        ]
    });
});
let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        inspectUnitList: [], // 检测机构列表
        devId: '',
        devType: '',
        specialDevCheckRecord: {
            remark: '',
            checkDate: '',
            certificateNumber: '',
            devId: '',
            devType: '',
            inspectUnitName: '', // 检测机构名称
            inspectUnitId: '', // 检测机构ID
            id: '',
        },
        ruleValidate: {
            certificateNumber: [
                {required: true, message: '登记证书编号必填', trigger: 'blur'}
            ]
        },
        q: {
            beginTime: '',
            endTime: ''
        }
    },
    created: function () {
        // 加载检测机构
        Ajax.request({
            url: "../inspectioninstitution/queryAll",
            async: true,
            successCallback: function (r) {
                vm.inspectUnitList = r.list;
            }
        });
    },
    methods: {
        // 查看合同证书
        goEquFile: function (moduleId) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-list-ul"></i> 附件列表',
                content: '../sys/equFile.html?fileTypeId=0669fb36cdb040e1a40f6d69f5003b68&moduleType=1&moduleId=' + moduleId,
                cancel: function (index) {
                    layer.close(index);
                    vm.reload();
                }
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "添加校验记录";
            vm.specialDevCheckRecord = {
                remark: '',
                checkDate: '',
                certificateNumber: '',
                devId: this.devId,
                devType: this.devType,
                inspectUnitName: '', // 检测机构名称
                inspectUnitId: '', // 检测机构ID
                id: '',
            }
        },
        update: function (event) {
            let checkRecord = getSelectedRowData("#jqGrid");
            if (!checkRecord) {
                return;
            }
            vm.showList = false;
            vm.title = "完善校验记录";
            vm.getInfo(checkRecord.id);
        },
        saveOrUpdate: function (event) {
            let url = vm.specialDevCheckRecord.id == (null || '') ? "../specialdevcheckrecord/save" : "../specialdevcheckrecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.specialDevCheckRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../specialdevcheckrecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.specialDevCheckRecord = r.specialDevCheckRecord;
                }
            });
        },
        reload: function (event) {
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            // 构建查询参数
            var param = {};
            if (vm.q.beginTime) {
                param.beginTime = gmt2DateStr(vm.q.beginTime);
            }
            if (vm.q.endTime) {
                param.endTime = vm.gmt2DateStr(vm.q.endTime);
            }
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: param,
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                beginTime: '',
                endTime: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            if (this.specialDevCheckRecord.actualCheckDate) {
                this.specialDevCheckRecord.actualCheckDate = gmt2DateStr(this.specialDevCheckRecord.actualCheckDate);
            }
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});